{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Diverging Stacked Bar Chart\n",
    "---------------------------\n",
    "This example shows a diverging stacked bar chart for sentiments towards a set of eight questions, displayed as percentages with neutral responses straddling the 0% mark.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "\n",
    "data =     [\n",
    "      {\n",
    "        \"question\": \"Question 1\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 24,\n",
    "        \"percentage\": 0.7,\n",
    "        \"percentage_start\": -19.1,\n",
    "        \"percentage_end\": -18.4\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 1\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 294,\n",
    "        \"percentage\": 9.1,\n",
    "        \"percentage_start\": -18.4,\n",
    "        \"percentage_end\": -9.2\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 1\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 594,\n",
    "        \"percentage\": 18.5,\n",
    "        \"percentage_start\": -9.2,\n",
    "        \"percentage_end\": 9.2\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 1\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 1927,\n",
    "        \"percentage\": 59.9,\n",
    "        \"percentage_start\": 9.2,\n",
    "        \"percentage_end\": 69.2\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 1\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 376,\n",
    "        \"percentage\": 11.7,\n",
    "        \"percentage_start\": 69.2,\n",
    "        \"percentage_end\": 80.9\n",
    "      },\n",
    "\n",
    "      {\n",
    "        \"question\": \"Question 2\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 18.2,\n",
    "        \"percentage_start\": -36.4,\n",
    "        \"percentage_end\": -18.2\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 2\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 18.2,\n",
    "        \"percentage_start\": -18.2,\n",
    "        \"percentage_end\": 0\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 2\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": 0,\n",
    "        \"percentage_end\": 0\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 2\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 7,\n",
    "        \"percentage\": 63.6,\n",
    "        \"percentage_start\": 0,\n",
    "        \"percentage_end\": 63.6\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 2\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 11,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": 63.6,\n",
    "        \"percentage_end\": 63.6\n",
    "      },\n",
    "\n",
    "      {\n",
    "        \"question\": \"Question 3\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 20,\n",
    "        \"percentage_start\": -30,\n",
    "        \"percentage_end\": -10\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 3\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": -10,\n",
    "        \"percentage_end\": -10\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 3\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 20,\n",
    "        \"percentage_start\": -10,\n",
    "        \"percentage_end\": 10\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 3\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 4,\n",
    "        \"percentage\": 40,\n",
    "        \"percentage_start\": 10,\n",
    "        \"percentage_end\": 50\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 3\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 20,\n",
    "        \"percentage_start\": 50,\n",
    "        \"percentage_end\": 70\n",
    "      },\n",
    "\n",
    "      {\n",
    "        \"question\": \"Question 4\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": -15.6,\n",
    "        \"percentage_end\": -15.6\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 4\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 12.5,\n",
    "        \"percentage_start\": -15.6,\n",
    "        \"percentage_end\": -3.1\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 4\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 1,\n",
    "        \"percentage\": 6.3,\n",
    "        \"percentage_start\": -3.1,\n",
    "        \"percentage_end\": 3.1\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 4\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 7,\n",
    "        \"percentage\": 43.8,\n",
    "        \"percentage_start\": 3.1,\n",
    "        \"percentage_end\": 46.9\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 4\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 6,\n",
    "        \"percentage\": 37.5,\n",
    "        \"percentage_start\": 46.9,\n",
    "        \"percentage_end\": 84.4\n",
    "      },\n",
    "\n",
    "      {\n",
    "        \"question\": \"Question 5\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": -10.4,\n",
    "        \"percentage_end\": -10.4\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 5\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 1,\n",
    "        \"percentage\": 4.2,\n",
    "        \"percentage_start\": -10.4,\n",
    "        \"percentage_end\": -6.3\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 5\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 3,\n",
    "        \"percentage\": 12.5,\n",
    "        \"percentage_start\": -6.3,\n",
    "        \"percentage_end\": 6.3\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 5\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 16,\n",
    "        \"percentage\": 66.7,\n",
    "        \"percentage_start\": 6.3,\n",
    "        \"percentage_end\": 72.9\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 5\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 4,\n",
    "        \"percentage\": 16.7,\n",
    "        \"percentage_start\": 72.9,\n",
    "        \"percentage_end\": 89.6\n",
    "      },\n",
    "\n",
    "      {\n",
    "        \"question\": \"Question 6\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 1,\n",
    "        \"percentage\": 6.3,\n",
    "        \"percentage_start\": -18.8,\n",
    "        \"percentage_end\": -12.5\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 6\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 1,\n",
    "        \"percentage\": 6.3,\n",
    "        \"percentage_start\": -12.5,\n",
    "        \"percentage_end\": -6.3\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 6\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 12.5,\n",
    "        \"percentage_start\": -6.3,\n",
    "        \"percentage_end\": 6.3\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 6\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 9,\n",
    "        \"percentage\": 56.3,\n",
    "        \"percentage_start\": 6.3,\n",
    "        \"percentage_end\": 62.5\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 6\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 3,\n",
    "        \"percentage\": 18.8,\n",
    "        \"percentage_start\": 62.5,\n",
    "        \"percentage_end\": 81.3\n",
    "      },\n",
    "\n",
    "      {\n",
    "        \"question\": \"Question 7\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": -10,\n",
    "        \"percentage_end\": -10\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 7\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": -10,\n",
    "        \"percentage_end\": -10\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 7\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 1,\n",
    "        \"percentage\": 20,\n",
    "        \"percentage_start\": -10,\n",
    "        \"percentage_end\": 10\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 7\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 4,\n",
    "        \"percentage\": 80,\n",
    "        \"percentage_start\": 10,\n",
    "        \"percentage_end\": 90\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 7\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": 90,\n",
    "        \"percentage_end\": 90\n",
    "      },\n",
    "\n",
    "      {\n",
    "        \"question\": \"Question 8\",\n",
    "        \"type\": \"Strongly disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": 0,\n",
    "        \"percentage_end\": 0\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 8\",\n",
    "        \"type\": \"Disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": 0,\n",
    "        \"percentage_end\": 0\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 8\",\n",
    "        \"type\": \"Neither agree nor disagree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": 0,\n",
    "        \"percentage_end\": 0\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 8\",\n",
    "        \"type\": \"Agree\",\n",
    "        \"value\": 0,\n",
    "        \"percentage\": 0,\n",
    "        \"percentage_start\": 0,\n",
    "        \"percentage_end\": 0\n",
    "      },\n",
    "      {\n",
    "        \"question\": \"Question 8\",\n",
    "        \"type\": \"Strongly agree\",\n",
    "        \"value\": 2,\n",
    "        \"percentage\": 100,\n",
    "        \"percentage_start\": 0,\n",
    "        \"percentage_end\": 100\n",
    "      }\n",
    "    ]\n",
    "\n",
    "\n",
    "color_scale = alt.Scale(\n",
    "            domain=[\"Strongly disagree\",\n",
    "            \"Disagree\",\n",
    "            \"Neither agree nor disagree\",\n",
    "            \"Agree\",\n",
    "            \"Strongly agree\"],\n",
    "            range=[\"#c30d24\", \"#f3a583\", \"#cccccc\", \"#94c6da\", \"#1770ab\"]\n",
    "        )\n",
    "\n",
    "y_axis = alt.Axis(title='Question',\n",
    "                  offset=5,\n",
    "                  ticks=False,\n",
    "                  minExtent=60,\n",
    "                  domain=False)\n",
    "\n",
    "source = alt.pd.DataFrame(data)\n",
    "\n",
    "alt.Chart(source).mark_bar().encode(\n",
    "    x='percentage_start:Q',\n",
    "    x2='percentage_end:Q',\n",
    "    y=alt.Y('question:N', axis=y_axis),\n",
    "    color=alt.Color(\n",
    "        'type:N',\n",
    "        legend=alt.Legend( title='Response'),\n",
    "        scale=color_scale,\n",
    "    )\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
